﻿using FDEEPublicFunc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FDEEMain
{
    public partial class PileRebarForm : Form
    {
        public string filePileName = "";
        public string dwgFileName = "";
        public string piersString = "";

        public string title = "";
        public int stNum = 0;
        public int endNum = 0;

        public string dawingNo1 = "";
        public int dawingNo2 = 1;
        public bool AutoMark = false;
        public int markRot = 270;
        public PileRebarForm()
        {
            InitializeComponent();
            cbLine.SelectedIndex = 0;
            lb.Visible = false;
            markRotate.Visible = false;
        }


        private void btnConfim_Click(object sender, EventArgs e)
        {
            title = cbLine.Text;
            if (string.IsNullOrWhiteSpace(dwgFileName))
            {
                MessageBox.Show("请选择平面图dwg文件！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(filePileName))
            {
                MessageBox.Show("请选择桩基信息表！", "提示");
                return;
            }          
            stNum =int.Parse(stNo.SelectedValue.ToString());
            endNum = int.Parse(endNo.SelectedValue.ToString()); 
            dawingNo1 = DrawingNo1.Text;
            dawingNo2 = int.Parse(DrawingNo2.Text);
            AutoMark = checkAutoMark.Checked;
            markRot = int.Parse(markRotate.Text);
            this.DialogResult = DialogResult.OK;
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }


        private void btnPlinth_Click(object sender, EventArgs e)
        {
            //打开文件
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = "csv";
            dlg.Filter = "csv Files|*.csv";
            if (dlg.ShowDialog() == DialogResult.OK)
                filePileName = dlg.FileName;
            if (filePileName == null)
                return;
            plinthPath.Text = filePileName;// new System.IO.FileInfo(fileName).Name;
            System.IO.FileInfo file = new System.IO.FileInfo(filePileName);
            System.Data.DataTable dt;
            if (file.Extension.Contains("xlsx"))
                dt = new FDEECommonTools.ExcelHelper().ExcelToTableForXLSX(filePileName);
            else
                dt = FDEECommonTools.CVSTools.OpenCSV(filePileName);
            dt.Columns[0].ColumnName = "NO";
            dt.Columns[2].ColumnName = "PierNo";
            DataView dv0 = dt.DefaultView;
        
            dt = dv0.ToTable(true, new string[] { "NO","PierNo" });
            if (dt.Rows.Count > 0)
            {
                stNo.Items.Clear();
                endNo.Items.Clear();
                stNo.DataSource = dt.Copy();
                endNo.DataSource = dt.Copy();
                stNo.DisplayMember = "PierNo";
                endNo.DisplayMember = "PierNo";
                stNo.ValueMember = "NO";
                endNo.ValueMember = "NO";

                stNo.SelectedIndex = 0;
                endNo.SelectedIndex = dt.Rows.Count - 1;
            }
        }      

        private void btnDwgSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = "dwg";
            dlg.Filter = "dwg Files|*.dwg";
            if (dlg.ShowDialog() == DialogResult.OK)
                dwgFileName = dlg.FileName;
            this.dwgFile.Text = dwgFileName;
        }

        private void checkAutoMark_CheckedChanged(object sender, EventArgs e)
        {
            if(checkAutoMark.Checked)
            {
                lb.Visible = true;
                markRotate.Visible = true;
            }
            else
            {
                lb.Visible = false;
                markRotate.Visible = false;
            }
        }
    }
}
